package com.jh.user.service;

import cn.jh.common.exception.ServiceException;
import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.TokenUtil;
import com.jh.user.business.*;
import com.jh.user.pojo.*;
import com.jh.user.pojo.enums.PayType;
import com.jh.user.util.CommonsUtil;
import com.jh.user.util.Util;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;




@Controller
@EnableAutoConfiguration
public class ThirdLevelDistriService {

	private static final Logger LOG = LoggerFactory.getLogger(ChannelService.class);

	@Autowired
	private BrandManageBusiness brandMangeBusiness;

	@Autowired
	private ThirdLeveDistributionBusiness  thirdLevelBusiness;

	@Autowired
	private UserLoginRegisterBusiness  userLoginRegisterBusiness;

	@Autowired
	private ChannelRateBusiness  channelRateBusiness;


	@Autowired
	Util util;

	@Autowired
	private UserBusiness userBusiness;

	/**新增一个三级分销产品*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/prod/add")
	public @ResponseBody Object addThirdLevelProd(HttpServletRequest request,
												  @RequestParam(value = "brand_id") long  brandid,
												  @RequestParam(value = "grade" ,defaultValue = "-1", required=false )  int   grade,
												  @RequestParam(value = "name") String name,
												  @RequestParam(value = "numOfPeople") int numOfPeople,
												  @RequestParam(value = "discount") String sdiscount,

												  //如何升级描述
												  @RequestParam(value = "upgrade_state", required=false) String upgradestate,

												  //是否线上购买
												  @RequestParam(value = "true_false_buy",   defaultValue = "0", required=false) String trueFalseBuy,

												  //享受收益描述TrueFalseBuy
												  @RequestParam(value = "earnings_state", required=false) String earningsState,
												  @RequestParam(value = "remark", required=false) String remark
	){
		Map<String,Object> map = new HashMap<String,Object>();
		long ldiscount;
		BigDecimal discount = null;

		try {
			ldiscount = Long.valueOf(sdiscount);
			discount = BigDecimal.valueOf(ldiscount,4);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "添加失败,参数discount不能为'"+ sdiscount +"',请检查后重试!");
			return map;
		}

		//long lmoney;
		//BigDecimal money = null;
		//try {
		//	lmoney = Long.valueOf(smoney);
		//	money = BigDecimal.valueOf(lmoney);
		//} catch (Exception e) {
		//	map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
		//	map.put(CommonConstants.RESP_MESSAGE, "添加失败,参数money不能为'"+ smoney +"',请检查后重试!");
		//	return map;
		//}

		ThirdLevelDistribution  distribution  =new ThirdLevelDistribution();
		if(grade==-1){
			distribution=null;
		}else{
			distribution= thirdLevelBusiness.getThirdLevelByBrandidandgradeNoStatus(brandid, grade);
		}

		if (distribution == null) {
			List<ThirdLevelDistribution> tlds=new ArrayList<ThirdLevelDistribution>();
			tlds=thirdLevelBusiness.getAllThirdLevelPrd(brandid);
//			if(tlds.size()>0){
//				grade=tlds.get(tlds.size()-1).getGrade()+1;
//			}else{
//				grade=tlds.size()+1;
//			}
			distribution =  new ThirdLevelDistribution();
			distribution.setBrandId(brandid);
			distribution.setCreateTime(new Date());
			distribution.setDiscount(discount);
			distribution.setGrade(tlds.size()+1);
			//distribution.setMoney(money);
			distribution.setName(name);
			distribution.setRemark(remark);
			distribution.setTrueFalseBuy(trueFalseBuy);
			distribution.setUpgradestate(upgradestate==null?"":upgradestate);
			distribution.setEarningsState(earningsState==null?"":earningsState);
			distribution.setStatus("0");
			distribution.setNumOfPeople(numOfPeople);
		} else {
			distribution.setCreateTime(new Date());
			distribution.setDiscount(discount);
			distribution.setGrade(grade);
			//distribution.setMoney(money);
			distribution.setName(name);
			distribution.setRemark(remark);
			distribution.setUpgradestate(upgradestate==null?"":upgradestate);
			distribution.setEarningsState(earningsState==null?"":earningsState);
			distribution.setTrueFalseBuy(trueFalseBuy);
			distribution.setStatus("0");
			distribution.setNumOfPeople(numOfPeople);
		}

		distribution = thirdLevelBusiness.mergeThirdDistribution(distribution);


		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, distribution);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		return map;


	}



	/**新增一个三级分销产品*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/prod/truefalsebuy/update")
	public @ResponseBody Object addThirdLevelProd(HttpServletRequest request,

												  //三级产品
												  @RequestParam(value = "id") long id,

												  //是否线上购买
												  @RequestParam(value = "true_false_buy",   defaultValue = "0", required=false) String trueFalseBuy
	){
		Map map = new HashMap();
		ThirdLevelDistribution  distribution  =thirdLevelBusiness.queryThirdLevelDistribution(id);

		if(distribution == null){
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询失败");
			return map;
		}else{
			distribution.setTrueFalseBuy(trueFalseBuy);
		}

		distribution = thirdLevelBusiness.mergeThirdDistribution(distribution);
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, distribution);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		return map;


	}


	/**新增品牌上工一个三级分销比率*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/ratio/add")
	public @ResponseBody Object addThirdLevelRatio(HttpServletRequest request,
												   @RequestParam(value = "brand_id") String  sbrandid,
												   @RequestParam(value = "ratio") BigDecimal ratio,
												   @RequestParam(value = "pre_level") String prelevel){
		Map<String,Object> map = new HashMap<String,Object>();
		if("".equals(sbrandid)){
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲.更新失败,贴牌号不能为:"+ sbrandid +",请检查后重试!!!");
			return map;
		}

		long brandid = 1;
		try {
			brandid = Long.valueOf(sbrandid);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "亲.更新失败,贴牌号不能为:"+ sbrandid +",请检查后重试!!!");
			return map;
		}

		List<ThirdLevelRebateRatio>  levelRebateRatios=thirdLevelBusiness.getAllThirdRatio(brandid);
		ThirdLevelRebateRatio levelRebateRatio = thirdLevelBusiness.getThirdRatioByBrandidandprelevel(brandid, prelevel);
		if( ratio.doubleValue()>0.33){
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "更新失败,总费率大于1");
			return map;
		}

		if(levelRebateRatio == null){
			levelRebateRatio =  new ThirdLevelRebateRatio();
			levelRebateRatio.setBrandId(brandid);
			levelRebateRatio.setRatio(ratio);
			levelRebateRatio.setPreLevel(prelevel);
			levelRebateRatio.setCreateTime(new Date());
		}else{

			levelRebateRatio.setRatio(ratio);
			levelRebateRatio.setCreateTime(new Date());
		}

		levelRebateRatio = thirdLevelBusiness.mergeThirdLevelRebateRatio(levelRebateRatio);

		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, levelRebateRatio);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		return map;
	}

	/**根据三级分销的id获取相信信息*/
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/user/thirdlevel/prod/query/{id}")
	public @ResponseBody Object queryThirdLevelProByid(HttpServletRequest request,
													   @PathVariable("id") long id){
		Map map = new HashMap();
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, thirdLevelBusiness.queryThirdLevelDistribution(id));
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		return map;
	}

	/**按品牌查询所有三级分销产品*/
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/user/thirdlevel/prod/brand/{brand}/{token}")
	public @ResponseBody Object queryThirdLevelPro (HttpServletRequest request,
												   @RequestParam(value = "access_type",defaultValue="1", required=false) long accesstype,
												   @PathVariable("brand") long brandid,
													@PathVariable("token") String token) {

		Map<String, Object> resMap = new HashMap<>();
		long userId = 0l;
		try {
			userId = TokenUtil.getUserId(token);
		} catch (Exception e) {
			return CommonsUtil.returnError("token无效");
		}

		Map<String,Object> map = new HashMap<String,Object>();
		if ("".equals(accesstype)||"".equals(brandid)) {
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询产品失败,设置条件不能为空!!!");
			return CommonsUtil.returnError(map.toString());
		}

		List<ThirdLevelDistribution> tld = thirdLevelBusiness.getAllThirdLevelPrd(brandid);
		if (tld.size() > 0 && accesstype != 1) {
			Brand brand = brandMangeBusiness.findBrandById(brandid);

			ThirdLevelDistribution ThirdLevelDistribution = new ThirdLevelDistribution();
			ThirdLevelDistribution.setBrandId(brandid);
			ThirdLevelDistribution.setCreateTime(new Date());
			ThirdLevelDistribution.setId(0);
			ThirdLevelDistribution.setGrade(0);
			ThirdLevelDistribution.setName(brand != null ? brand.getLevelName() : "");
			ThirdLevelDistribution.setMoney(new BigDecimal("0.0"));
			ThirdLevelDistribution.setDeposit(new BigDecimal("0.0"));
			ThirdLevelDistribution.setDeposit(new BigDecimal("0.0"));
			ThirdLevelDistribution.setStatus("0");
			ThirdLevelDistribution.setTrueFalseBuy("0");
			ThirdLevelDistribution.setNumOfPeople(0);
			ThirdLevelDistribution.setNumOfPeople(0);
			tld.add(ThirdLevelDistribution);
		}

		int count = userBusiness.findUserListByPreUserId(userId);
		LOG.info("userId = " + userId + "用户下推荐人总数为：" + count);
		User user = userBusiness.findUserById(userId);
		String currentGrade = null == user.getGrade() ? "0" : user.getGrade();
		// 测试数据
		// resMap.put("count", 10);
		resMap.put("count", count);
		resMap.put("tld", tld);
		resMap.put("currentGrade", currentGrade);
		JSONObject jsonObject = JSONObject.fromObject(resMap);
		LOG.info("按品牌查询所有三级分销产品 ThirdLevelDistriService.queryThirdLevelPro 结果： " + jsonObject);
		return CommonsUtil.returnSuccess(jsonObject);
	}

	/**按品牌查询所有三级分销费率 */
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/user/thirdlevel/prod/rate/{levelid}")
	public @ResponseBody Object queryThirdLevelRates(HttpServletRequest request,
													 @PathVariable("levelid") long levelid){
		Map map = new HashMap();
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, thirdLevelBusiness.findAllThirdLevelRates(levelid));
		map.put(CommonConstants.RESP_MESSAGE, "成功");

		return map;
	}

	/**按品牌查询所有三级分销费率 */
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/channel/rate")
	public @ResponseBody Object queryThirdLevelRatesbylevelidAndChannelId(HttpServletRequest request,
																		  @RequestParam("thirdLevelId") long thirdLevelId,
																		  @RequestParam("channelId") long channelId){
		Map<String, Serializable> map = new HashMap<String, Serializable>();
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, thirdLevelBusiness.findAllThirdLevelRatesBylevelidAndChannelId(thirdLevelId, channelId));
		map.put(CommonConstants.RESP_MESSAGE, "成功");

		return map;
	}

	/**按品牌添加三级分销费率 */
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/prod/rate/add")
	public @ResponseBody Object addThirdLevelRates(HttpServletRequest request,
												   @RequestParam("thirdLevelId") long thirdLevelId,
												   @RequestParam("channelId") long channelId,
												   @RequestParam("rate") String srate
//			@RequestParam("extraFee") BigDecimal extraFee
	){
		Map<String,Object> map = new HashMap<String,Object>();
		BigDecimal rate = null;
		try {
			rate = new BigDecimal(Double.valueOf(srate)).setScale(4, BigDecimal.ROUND_HALF_DOWN);
		} catch (NumberFormatException e) {
//			e.printStackTrace();
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "添加失败,您设置的费率有误,请检查后重试!");
			return map;
		}

		ThirdLevelRate tlr = thirdLevelBusiness.findAllThirdLevelRatesBylevelidAndChannelId(thirdLevelId, channelId);

		ThirdLevelDistribution thirdLevelDistribution=thirdLevelBusiness.queryThirdLevelDistribution(thirdLevelId);


		List<ThirdLevelDistribution> tlds=new ArrayList<ThirdLevelDistribution>();
		BrandRate brandRate=null;
		if(thirdLevelDistribution!=null){

			brandRate=brandMangeBusiness.findRateByBrandAndChannel(thirdLevelDistribution.getBrandId(),channelId,"0", PayType.TRADE.getValue());
			if(brandRate!=null){
				if(rate.doubleValue()<brandRate.getMinrate().doubleValue()){
					map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
					map.put(CommonConstants.RESP_MESSAGE, "添加失败,您的设置的费率小于该通道的最低费率"+brandRate.getMinrate());
					return map;
				}
				if(rate.doubleValue()>brandRate.getRate().doubleValue()){
					map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
					map.put(CommonConstants.RESP_MESSAGE, "添加失败,您的设置的费率大于该通道的普通会员的费率"+brandRate.getRate());
					return map;
				}

			}else{
				map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
				map.put(CommonConstants.RESP_MESSAGE, "添加失败,您的贴牌未设置初始费率");
				return map;

			}

			tlds=thirdLevelBusiness.getAllThirdLevelPrd(thirdLevelDistribution.getBrandId());
			if(tlds.size()>0&&tlds.size()>thirdLevelDistribution.getGrade()){
				ThirdLevelDistribution thirdLevelDistribution2=thirdLevelBusiness.getThirdLevelByBrandidandgrade(thirdLevelDistribution.getBrandId(), thirdLevelDistribution.getGrade()+1);
				if(thirdLevelDistribution2!=null){
					ThirdLevelRate tlr2 =thirdLevelBusiness.findAllThirdLevelRatesBylevelidAndChannelId(thirdLevelDistribution2.getId(), channelId);
					if(tlr2!=null){
						if(tlr2.getRate().doubleValue()>rate.doubleValue()){
							map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
							map.put(CommonConstants.RESP_MESSAGE, "添加失败,你设置的费率小于你的上一等级产品个费率");
							return map;
						}
					}else{
						map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
						map.put(CommonConstants.RESP_MESSAGE, "添加失败,你的最高级别产品的费率还没设置");
						return map;

					}
					if(thirdLevelDistribution.getGrade()>1){
						ThirdLevelDistribution thirdLevelDistribution3=thirdLevelBusiness.getThirdLevelByBrandidandgrade(thirdLevelDistribution.getBrandId(), thirdLevelDistribution.getGrade()-1);
						if(thirdLevelDistribution3!=null){
							ThirdLevelRate tlr3=thirdLevelBusiness.findAllThirdLevelRatesBylevelidAndChannelId(thirdLevelDistribution3.getId(), channelId);
							if(tlr3!=null){
								if(tlr3.getRate().doubleValue()<rate.doubleValue()){
									map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
									map.put(CommonConstants.RESP_MESSAGE, "添加失败,你设置的费率大于你的下一等级产品个费率");
									return map;
								}
							}
						}

					}
				}
			}

		}else{
			map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "添加失败,你设置产品不存在");
			return map;

		}

		if(tlr==null){
			tlr=new ThirdLevelRate();
		}
		tlr.setThirdLevelId(thirdLevelId);
		tlr.setChannelId(channelId);
		tlr.setRate(rate);
		tlr.setExtraFee(brandRate.getExtraFee());
		tlr.setCreateTime(new Date());
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT,thirdLevelBusiness.addThirdLevelRates(tlr));
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		/****
		 * 替换用户等级
		 * ***/
		{
			/***
			 * 查询该等级的用户
			 * ***/
			Long[] userIds=userLoginRegisterBusiness.queryUserIdByGradeAndBrandId(thirdLevelDistribution.getBrandId(), thirdLevelDistribution.getGrade()+"");
			Brand brand=brandMangeBusiness.findBrandById(thirdLevelDistribution.getBrandId());
			/***
			 * 进行替换
			 * ***/
			if(userIds!=null&&userIds.length>0){
				for(Long userid:userIds){
					if(userid==brand.getManageid()){
						continue;
					}
					ChannelRate channelRate=channelRateBusiness.findChannelRateByUserid(userid, channelId);
					if(channelRate==null){
						channelRate=new ChannelRate();
						channelRate.setUserId(userid);
						channelRate.setBrandId(thirdLevelDistribution.getBrandId());
						channelRate.setChannelId(tlr.getChannelId());
						channelRate.setRate(tlr.getRate());
						channelRate.setExtraFee(brandRate.getExtraFee());
						channelRate.setGradeRate(brandRate.getGradeRate());
						channelRate.setCreateTime(new Date());
					}else{
						channelRate.setRate(rate);
						channelRate.setExtraFee(brandRate.getExtraFee());
						channelRate.setGradeRate(brandRate.getGradeRate());
						channelRate.setCreateTime(new Date());
					}

					channelRateBusiness.mergeChannelRate(channelRate);
				}
			}



		}
		return map;
	}

	/**按品牌查询所有三级分销比率*/
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/user/thirdlevel/ratio/query/{brand}")
	public @ResponseBody Object queryThirdLevelRatio(HttpServletRequest request,
													 @PathVariable("brand") long brandid
	){
		Map<String,Object> map = new HashMap<String,Object>();
		List<ThirdLevelRebateRatio> models = null;
		models = thirdLevelBusiness.getAllThirdRatio(brandid);
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, models);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		return map;
	}

	/**按品牌查询所有三级分销比率*/
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/user/thirdlevel/prod/ratio/query/{brand}")
	public @ResponseBody Object queryThirdLevelProdRatioBy(HttpServletRequest request,
														   @PathVariable("brand") long brandid){
		Map map = new HashMap();
		List<ThirdLevelDistribution> thirdLevelDistributions= thirdLevelBusiness.getAllThirdLevelPrd(brandid);
		if(thirdLevelDistributions==null){
			map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
			map.put(CommonConstants.RESULT, thirdLevelBusiness.getAllThirdRatio(brandid));
			map.put(CommonConstants.RESP_MESSAGE, "你还未添加产品，无法配置返佣费率");
			return map;
		}
		List<Map> maps=new ArrayList<Map>();

		List<ThirdLevelRebateRatio> thirdLevelRebateRatios =thirdLevelBusiness.getAllThirdRatio(brandid);
		for(ThirdLevelDistribution thirdLevelDistribution:thirdLevelDistributions){
			Map thirdLevelProdRatio=new HashMap();
			thirdLevelProdRatio.put("thirdLevelId", thirdLevelDistribution.getId());
			thirdLevelProdRatio.put("thirdLevelNmae", thirdLevelDistribution.getName());
			thirdLevelProdRatio.put("thirdLevelBrandId", thirdLevelDistribution.getBrandId());
			thirdLevelProdRatio.put("thirdLevelGrade", thirdLevelDistribution.getGrade());
			thirdLevelProdRatio.put("thirdLevelRatio", "");
			if(thirdLevelRebateRatios!=null){
				for(ThirdLevelRebateRatio thirdLevelRebateRatio:thirdLevelRebateRatios){
					if(Integer.parseInt(thirdLevelRebateRatio.getPreLevel())==thirdLevelDistribution.getGrade()){
						thirdLevelProdRatio.put("thirdLevelRatio", thirdLevelRebateRatio.getRatio());
					}
				}
			}
			maps.add(thirdLevelProdRatio);
		}



		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, maps);
		map.put(CommonConstants.RESP_MESSAGE, "成功");

		return map;
	}

	/**
     * 查询三级分销所有通道费率信息(NEW)
     */
    @RequestMapping(method=RequestMethod.POST,value="/v1.0/user/third_level/query/channel_rate")
    public @ResponseBody Object findThirdLevelChannelRate(@RequestParam("thirdLevelId") Long thirdLevelId,
                                                          @RequestParam(value="brandId", required=false) Long brandId){
        Map map = new HashMap();
        try {
            ThirdLevelDistribution thirdLevelDistribution= thirdLevelBusiness.queryThirdLevelDistribution(thirdLevelId);
            List<Channel> channels;
            if(thirdLevelId!=0) {
                channels = channelRateBusiness.findAllChannelByBrandid(thirdLevelDistribution.getBrandId());
            }else {
                channels = channelRateBusiness.findAllChannelByBrandid(brandId);
            }
            List<Channel> result = new ArrayList<Channel>();
            if(thirdLevelId==0) {
                result=channels;
            }else {
                for(Channel channel : channels){
                    Channel temp = channel;
                    ThirdLevelRate tlr = thirdLevelBusiness.findAllThirdLevelRatesBylevelidAndChannelId(thirdLevelId,  channel.getId());
                    BrandRate br = brandMangeBusiness.findRateByBrandAndChannel(thirdLevelDistribution.getBrandId(), channel.getId(),"0", PayType.WITHDRAW.getValue());
                    if(tlr == null){
                        temp.setRate(null);
                    }else{
                        temp.setRate(tlr.getRate());
                        temp.setExtraFee(br.getExtraFee());
                        temp.setWithdrawFee(br.getExtraFee());
                    }
                    result.add(temp);
                }
            }
            map.put(CommonConstants.RESULT, result);
            map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
            map.put(CommonConstants.RESP_MESSAGE, "成功");

        } catch (Exception e) {
            LOG.error("/v1.0/user/thirdlevel/rate/query/thirdlevelid:"+e);
            map.put(CommonConstants.RESP_CODE,CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "查询失败，请稍后重试");
            map.put(CommonConstants.RESULT, null);
        }

        return map;

    }

	/**通过等级查询费率*/
	/**传递产品id将所有平台信息和费率都取回来*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/rate/query/thirdlevelid")
	public @ResponseBody Object queryAllChannelByBrandId(@RequestParam(value = "grade",defaultValue="1", required=true) String grade,
														 @RequestParam(value = "access_type",defaultValue="1", required=false) long accesstype,
														 @RequestParam(value="brand_id" , defaultValue = "0", required=false) long brandid){
		try {
			List<Channel> list = channelRateBusiness.findAll(grade);
			List<ChannelVO> result = ChannelVO.transform(list);
			return CommonsUtil.returnSuccess(result);
		} catch (Exception e) {
			LOG.error("/v1.0/user/thirdlevel/rate/query/thirdlevelid:"+e);
			return CommonsUtil.returnError("查询等级费率失败！");
		}
	}

	/**通过等级查询费率*/
	/** app传递产品id将所有平台信息和费率都取回来*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/rate/query/app")
	public @ResponseBody Object queryAllChannelBy(@RequestParam(value = "grade",defaultValue="1", required=true) String grade,
														 @RequestParam(value = "access_type",defaultValue="1", required=false) long accesstype,
														 @RequestParam(value="brand_id" , defaultValue = "0", required=false) long brandid){
		try {
			return CommonsUtil.returnSuccess(channelRateBusiness.findAll(grade));
		} catch (Exception e) {
			LOG.error("/v1.0/user/thirdlevel/rate/query/thirdlevelid:"+e);
			return CommonsUtil.returnError("查询等级费率失败！");
		}
	}

	/**获取等级信息
	 * grade 等级
	 * brand_id 贴牌ID
	 * */
	@RequestMapping(method=RequestMethod.GET,value="/v1.0/user/thirdlevel/prod/grade/query/{token}")
	public @ResponseBody Object queryThirdLevelDistriByBrandId(HttpServletRequest request,
															   @PathVariable("token") String token){
		Map map = new HashMap();
		long userId;
		try {
			userId = TokenUtil.getUserId(token);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE,CommonConstants.ERROR_TOKEN);
			map.put(CommonConstants.RESP_MESSAGE, "token无效");
			return map;
		}

		User user =userLoginRegisterBusiness.queryUserById(userId);

		ThirdLevelDistribution  distribution  =  thirdLevelBusiness.getThirdLevelByBrandidandgradeNoStatus(user.getBrandId(),Integer.parseInt(user.getGrade()) );
		if(distribution==null){
			distribution=new ThirdLevelDistribution();
			distribution.setGrade(0);
			//distribution.setName("普通会员");
			distribution.setName("员工");
		}
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "成功");
		map.put(CommonConstants.RESULT, distribution);
		return map;
	}


	//根据最高等级删除一个三级分销产品
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/delete/thirdlevelid")
	public @ResponseBody Object deleteThirdLevelDistributionByGrade(HttpServletRequest request,
																	@RequestParam("brand_id") long brandId){

		//查找到最高等级
		int ftdb = thirdLevelBusiness.findThirdLevelDistributionByBrandid(brandId);

		List<User> users = new ArrayList<User>();
		users = userLoginRegisterBusiness.queryUserByGrade(brandId, ftdb+"");
		Map map = new HashMap();
		if(users!=null&&users.size()>0){
			map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_PARAM);
			map.put(CommonConstants.RESP_MESSAGE, "该产品已被用户购买使用，无法删除该产品");
		}else{
			//根据最高等级删除产品
			thirdLevelBusiness.deleteThirdLevelDistributionByGrade(ftdb,brandId);

			map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
			map.put(CommonConstants.RESP_MESSAGE, "删除成功");
		}


		return map;
	}

	/**按品牌和产品id查询所有三级分销比率/新返佣逻辑使用接口 */
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/ratio/query/by/thirdlevelid")
	public @ResponseBody Object queryThirdLevelRatioByBrandIdAndThirdLevelId(HttpServletRequest request,
																			 @RequestParam("brandId") String sbrandId,
																			 @RequestParam(value="thirdLevelId")String sthirdLevelId
	){
		Map<String,Object> map = new HashMap<String,Object>();
		long brandId;
		Integer thirdLevelId = null;
		try {
			brandId = Long.valueOf(sbrandId).longValue();
			thirdLevelId = Integer.valueOf(sthirdLevelId);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "修改失败,输入参数有误,请检查后重试!");
			return map;
		}
		List<ThirdLevelRebateRatioNew> models = null;
		models = thirdLevelBusiness.getAllThirdRatio(brandId,thirdLevelId);
		if(models == null || models.size() == 0){
			map.put(CommonConstants.RESP_MESSAGE, "查询成功,但没有数据!");
		}else{
			map.put(CommonConstants.RESULT, models);
			map.put(CommonConstants.RESP_MESSAGE, "查询成功!");
		}
		map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
		return map;
	}

	//	根据brandId查询出所有返佣比例设置,如果没有配置则进行初始化
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/ratio/query/all")
	public @ResponseBody Object queryAllThirdLevelRebateByBrandId(HttpServletRequest request,
																  @RequestParam(value="brandId") String sbrandId
	){
		Map<String,Object> map = new HashMap<String,Object>();
		Long brandId = null;
		if("".equals(brandId)){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询失败,输入参数有误,请检查后重试!");
			return map;
		}
		try {
			brandId = Long.valueOf(sbrandId);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询失败,输入参数有误,请检查再重试!");
			return map;
		}
		Brand brand = brandMangeBusiness.findBrandById(brandId);
		if(brand ==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询失败,无该贴牌!");
			return map;
		}
		List<ThirdLevelDistribution> proModels = null;
		List<ThirdLevelRebateRatioNew> ratioModels = null;
		proModels = thirdLevelBusiness.getAllThirdLevelPrd(brandId);
		if(proModels==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "查询失败,无产品配置!");
			return map;
		}
		for(int i = 0; i < proModels.size() ;i++){
			ratioModels = thirdLevelBusiness.getAllThirdRatio(brandId,Integer.valueOf(proModels.get(i).getId()+""));
			if(ratioModels.size() != proModels.size()){
				for(int j = 0; j < proModels.size() ;j++){
					ThirdLevelRebateRatioNew ratioModel = thirdLevelBusiness.getByBrandIdAndPreLevelAndThirdLevelId(brandId,Integer.valueOf(proModels.get(i).getId()+""),j+1+"");
					if(ratioModel == null){
						ratioModel = new ThirdLevelRebateRatioNew();
						ratioModel.setBrandId(brandId);
						ratioModel.setPreLevel(j+1+"");
						ratioModel.setThirdLevelId(Integer.valueOf(proModels.get(i).getId()+""));
						ratioModel.setRatio(BigDecimal.ZERO.setScale(4, BigDecimal.ROUND_HALF_UP));
						ratioModel = thirdLevelBusiness.mergeThirdLevelRebateRatio(ratioModel);
					}
				}
			}
		}
		ratioModels = thirdLevelBusiness.getAllThirdRatioByBrandId(brandId);
		map.put("brand", brand);
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put("ThirdLevelDistribution", proModels);
		map.put(CommonConstants.RESP_MESSAGE, "查询成功");
		map.put(CommonConstants.RESULT, ratioModels);
		return map;
	}

	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/ratio/set/by/id")
	public @ResponseBody Object setThirdRatio(HttpServletRequest request,
											  @RequestParam(value="thirdRatioId")String sthirdRatioId,
											  @RequestParam(value="ratio")String sratio
	){
		Map<String,Object>map = new HashMap<String,Object>();
		Long thirdRatioId = null;
		BigDecimal ratio = null;
		if("".equals(sthirdRatioId.trim())||"".equals(sratio.trim())){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "修改失败,输入参数有误,请检查后重试!");
			return map;
		}
		try {
			thirdRatioId = Long.valueOf(sthirdRatioId);
			ratio = BigDecimal.valueOf(Double.valueOf(sratio)).setScale(4, BigDecimal.ROUND_HALF_UP);
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "修改失败,输入参数有误,请检查后再试!");
			return map;
		}
		if(ratio.compareTo(BigDecimal.ZERO) < 0 || ratio.compareTo(BigDecimal.ONE) > 0){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "修改失败,输入返佣比例有误,只能大于0小于1,请检查后再试!");
			return map;
		}

		ThirdLevelRebateRatioNew model = null;
		model = thirdLevelBusiness.getThirdRatioById(thirdRatioId);
		if(model==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "修改失败,无需要修改的配置!");
			return map;
		}
		model.setRatio(ratio);
		model = thirdLevelBusiness.mergeThirdLevelRebateRatio(model);
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "修改成功!");
		map.put(CommonConstants.RESULT, model);
		return map;
	}

	@RequestMapping(method=RequestMethod.POST,value="/v1.0/user/thirdlevel/ratio/onoff")
	public @ResponseBody Object updateNewRebate(HttpServletRequest request,
												@RequestParam(value="brandId")String sbrandId,
												@RequestParam(value="status")String status
	){
		Map<String,Object>map = new HashMap<String,Object>();
		Brand model = null;
		model = brandMangeBusiness.findBrandById(Long.valueOf(sbrandId));
		if(model==null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "设置失败,无该贴牌!");
			return map;
		}
		Integer isNewRebate = null;
		try {
			isNewRebate = Integer.valueOf(status);
			if(0!=isNewRebate.intValue() && 1!=isNewRebate.intValue()){
				map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
				map.put(CommonConstants.RESP_MESSAGE, "设置失败,设置参数有误,请检查后重试!");
				return map;
			}
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "设置失败,设置参数非法,请检查后重试!");
			return map;
		}
		model.setIsNewRebate(isNewRebate);
		model = brandMangeBusiness.mergeBrand(model);
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "设置成功");
		map.put(CommonConstants.RESULT,model);
		return map;
	}

	/**
	 * 根据 brandId 查询等级信息
	 * @param token
	 * @param brandId
	 * @return
	 */
	@PostMapping(value = "/v1.0/user/thirdlevel/queryThirdLevelByBrandId/{token}")
	public @ResponseBody Object queryThirdLevelListByBrandId(@PathVariable(value = "token") String token,
														 @RequestParam(value = "brandId") long brandId) {
		try {
			TokenUtil.getUserId(token);
		} catch (Exception e) {
			return CommonsUtil.returnError("token无效！");
		}
		try {
			List<Map<String, Object>> thirdLevelList = thirdLevelBusiness.queryThirdLevelListByBrandId(brandId);
			return CommonsUtil.returnSuccess(thirdLevelList, "查询成功！");
		} catch (ServiceException e) {
			return CommonsUtil.returnError(e.getMessage());
		} catch (Exception e) {
			return CommonsUtil.returnError("服务器异常，请稍后再试！");
		}
	}
}

